/*
当庄模式牌局流水记录界面
*/
var confige = require("confige");
var PaiGowLogic = require("PaiGowLogic");

cc.Class({
    extends: cc.Component,

    properties: {
        history_item:{
            default:null,
            type:cc.Node
        },
        isInit:false,
    },

    onLoad: function () {

    },

    onInit:function(){
        this.historyView1 = this.node.getChildByName("historyView1");
        this.historyView2 = this.node.getChildByName("historyView2");
        this.historyContent1 = this.historyView1.getChildByName("view").getChildByName("content");
        this.historyContent2 = this.historyView2.getChildByName("view").getChildByName("content");
        this.settleLayer = this.node.getChildByName("settleLayerBankerMode").getComponent("settleLayer");
        this.settleLayer.onInit();

        this.historyItemBeginY = -15;
        this.historyItemOffsetY = -145;

        this.historyItemList1 = {};
        this.historyItemList2 = {};

        this.gameHistoryData = {};
        this.isInit = true;
    },
    //刷新总流水界面
    updateHistory1:function(){
        for(var i in this.historyItemList1)
        {
            this.historyItemList1[i].destroy();
        }
        this.historyItemList1 = {};
        console.log(this.gameHistoryData);
        var newContentHeight = 0;
        var historyItemCount = 0;
        var newHistoryItemY = this.historyItemBeginY;
        for(var i in this.gameHistoryData)
        {
            if(!this.gameHistoryData[i][0])
                continue;
            historyItemCount++;

            var curRoomData = this.gameHistoryData[i];
            console.log("curRoomData======");
            console.log(curRoomData);

            var newHisPlayerCount = 0;
            for(var j in curRoomData)
                newHisPlayerCount ++;
            var newHistoryItem = cc.instantiate(this.history_item);
            newHistoryItem.y = newHistoryItemY;
            this.historyItemList1[i] = newHistoryItem;
            this.historyContent1.addChild(newHistoryItem);
            newHistoryItem.active = true;
            newHistoryItemY += this.historyItemOffsetY;

            var self = this;
            var btnShow = newHistoryItem.getChildByName("mainBg").getComponent("cc.Button");
            var showCallBack = function(){
                console.log("showCallBack!!!!!!!!!!!");
                self.showSettle(showCallBack.curData);
            };
            showCallBack.curData = curRoomData;
            btnShow.node.on(cc.Node.EventType.TOUCH_END, showCallBack, self);

            var hisTime = newHistoryItem.getChildByName("time").getComponent("cc.Label");
            hisTime.string = window.DKTools.getDateDay(curRoomData[0].time) + "   " + window.DKTools.getDateTime(curRoomData[0].time);
            var winScore = newHistoryItem.getChildByName("winScore");
            var loseScore =  newHistoryItem.getChildByName("loseScore");
            if(curRoomData[0].curScore >= 0 )
            {
                winScore.active = true;
                winScore.getComponent("cc.Label").string = "/" + curRoomData[0].curScore;
            }else{
                loseScore.active = true;
                loseScore.getComponent("cc.Label").string = curRoomData[0].curScore;
            }
            var handCardList = newHistoryItem.getChildByName("handCardList");
            var jIndex = 0
            for(var j=0;j<9;j++)
            {
                if(curRoomData[j] != null)
                {
                    var curCardIndex1 = curRoomData[j].handCard[0].num;
                    var curCardIndex2 = curRoomData[j].handCard[1].num;
                    var curType = PaiGowLogic.getType(curRoomData[j].handCard).type;
                    if(curType>10)
                        curType = 10;
                    var newCardNode = handCardList.getChildByName("cardNode"+jIndex);
                    newCardNode.active = true;
                    newCardNode.getChildByName("Card0").getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[curCardIndex1];
                    newCardNode.getChildByName("Card1").getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[curCardIndex2];
                    newCardNode.getChildByName("type").getComponent("cc.Sprite").spriteFrame = confige.paiGowTypeFrameMap[curType];
                    jIndex++;
                }
            }
        }
        if(newContentHeight < 450)
            newContentHeight = 450;
        this.historyContent1.height = -newHistoryItemY + 100;
    },
    //刷新我的流水界面
    updateHistory2:function(){
        for(var i in this.historyItemList2)
        {
            this.historyItemList2[i].destroy();
        }
        this.historyItemList2 = {};
        console.log(this.gameHistoryData);
        var newContentHeight = 0;
        var historyItemCount = 0;
        var newHistoryItemY = this.historyItemBeginY;
        for(var i in this.gameHistoryData)
        {
            if(!this.gameHistoryData[i][0])
                continue;
            var curRoomData = this.gameHistoryData[i];
            var continueType = true;
            for(var j in curRoomData)
                if(curRoomData[j].uid == confige.userInfo.playerId)
                    continueType = false;
            if(continueType)
                continue;
            historyItemCount++;
            console.log("curRoomData======");
            console.log(curRoomData);

            var newHisPlayerCount = 0;
            for(var j in curRoomData)
                newHisPlayerCount ++;
            var newHistoryItem = cc.instantiate(this.history_item);
            newHistoryItem.y = newHistoryItemY;
            this.historyItemList2[i] = newHistoryItem;
            this.historyContent2.addChild(newHistoryItem);
            newHistoryItem.active = true;
            newHistoryItemY += this.historyItemOffsetY;

            var self = this;
            var btnShow = newHistoryItem.getChildByName("mainBg").getComponent("cc.Button");
            var showCallBack = function(){
                console.log("showCallBack!!!!!!!!!!!");
                self.showSettle(showCallBack.curData);
            };
            showCallBack.curData = curRoomData;
            btnShow.node.on(cc.Node.EventType.TOUCH_END, showCallBack, self);

            var hisTime = newHistoryItem.getChildByName("time").getComponent("cc.Label");
            hisTime.string = window.DKTools.getDateDay(curRoomData[0].time) + "   " + window.DKTools.getDateTime(curRoomData[0].time);
            var winScore = newHistoryItem.getChildByName("winScore");
            var loseScore =  newHistoryItem.getChildByName("loseScore");
            if(curRoomData[0].curScore >= 0 )
            {
                winScore.active = true;
                winScore.getComponent("cc.Label").string = "/" + curRoomData[0].curScore;
            }else{
                loseScore.active = true;
                loseScore.getComponent("cc.Label").string = curRoomData[0].curScore;
            }
            var handCardList = newHistoryItem.getChildByName("handCardList");
            var jIndex = 0
            for(var j=0;j<9;j++)
            {
                if(curRoomData[j] != null)
                {
                    var curCardIndex1 = curRoomData[j].handCard[0].num;
                    var curCardIndex2 = curRoomData[j].handCard[1].num;
                    var curType = PaiGowLogic.getType(curRoomData[j].handCard).type;
                    if(curType>10)
                        curType = 10;
                    var newCardNode = handCardList.getChildByName("cardNode"+jIndex);
                    newCardNode.active = true;
                    newCardNode.getChildByName("Card0").getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[curCardIndex1];
                    newCardNode.getChildByName("Card1").getComponent("cc.Sprite").spriteFrame = confige.paiGowCardFrameMap[curCardIndex2];
                    newCardNode.getChildByName("type").getComponent("cc.Sprite").spriteFrame = confige.paiGowTypeFrameMap[curType];
                    jIndex++;
                }
            }
        }
        if(newContentHeight < 450)
            newContentHeight = 450;
        this.historyContent2.height = -newHistoryItemY + 100;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();

        this.updateHistory1();
        this.updateHistory2();
        this.node.active = true;
    },

    hideLayer:function(){
        this.node.active = false;
    },
    //切换显示模式(1->总流水，0->我的流水)
    changeType:function(event, customEventData){
        var type = parseInt(customEventData);
        if(type)
        {
            this.historyView1.active = true;
            this.historyView2.active = false;
        }else{
            this.historyView1.active = false;
            this.historyView2.active = true;
        }
    },
    //显示某条流水记录的详细信息界面
    showSettle:function(data){
        var curMeChair = 0;
        for(var i=0;i<9;i++)
            if(data[i] != null)
                if(data[i].uid == confige.userInfo.playerId)
                    curMeChair = i;
        var newMyScore = null;
        var myScoreCount = 0;
        var newSitScore = 0;
        for(var i=0;i<9;i++)
        {
            if(data[i] != null)
            {
                if(i != 0)
                {
                    if(data.betList[i][curMeChair] == null)
                        newMyScore = 0;
                    else
                        newMyScore = data.betList[i][curMeChair]*this.getScoreType(data.resultState[i]);

                    myScoreCount += newMyScore;
                    var betCount = 0;
                    for(var j in data.betList[i])
                        betCount += data.betList[i][j];
                    newSitScore = betCount * this.getScoreType(data.resultState[i]);
                }else{
                    newSitScore = data[0].curScore;
                }
                var niuType = data[i].result.type;
                if(data[i].nickname != null)
                    this.settleLayer.addOneSettle(data[i].nickname, niuType, newSitScore,2,data[i].handCard,i,newMyScore);
                else
                    this.settleLayer.addOneSettle("空闲", niuType, newSitScore,2,data[i].handCard,i,newMyScore);
            }
        }
        if(curMeChair == 0)
            myScoreCount = data[0].curScore;
        this.settleLayer.showLayer();
        this.settleLayer.showMyScore(myScoreCount);
    },
    //输赢状态的转换
    getScoreType:function(str){
        if(str == "win")
            return -1;
        if(str == "lose")
            return 1;
        if(str == "tie")
            return 0;
    },
});
